package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.zhijiaoyun.common.ResponseUtils;
import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.dao.ILogDao;
import com.zhijiaoyun.model.Log;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.ILogService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "logService")
public class LogServiceImpl implements ILogService {

	@Resource
	private ILogDao logDao;

	@Override
	public void insertLog(Log log) {
		logDao.insert(log);
	}

	@Override
	public PageList<Log> pageByParams(Integer pageIndex, int pageSize, String username) {
		int count = logDao.countByParams(username);
		PageData pageData = new PageData();
		List<Log> list = new ArrayList<>();
		PageList<Log> pageList = new PageList<>();
		if (count > 0) {
			int pageIndexInt = pageIndex == null ? 1 : pageIndex;
			pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
			list = logDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), username);
			pageList = new PageList<>(list, pageData);
		}
		return pageList;
	}

	@Override
	public Map<String, Object> page(int pageIndex, int pageSize, String username) {
		PageList<Log> logs = pageByParams(pageIndex, pageSize, username);
		Map<String, Object> map = new HashMap<>();
		map.put(ResponseUtils.PAGE, logs.getPageData());
		map.put(ResponseUtils.LIST, logs.getList());
		return ResponseUtils.responseSuccessMap(map);
	}

}
